package com.logicwin.RobotTravel.Navigate.messages;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.logicwin.RobotTravel.Navigate.ScreenNavigateImpl;
import com.logicwin.RobotTravel.Services.Impl.ServiceManager;

import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.util.Log;

public class EventWifiScanResult extends BaseEvent{
	private List<ScanResult> mResults;
	private ScreenNavigateImpl mImpl;
	private final static String TAG = EventWifiScanResult.class.getCanonicalName();
	public EventWifiScanResult(ScreenNavigateImpl impl, List<ScanResult> results){
		super(BaseEvent.EVENT_TYPE.WIFI_RESULTS);
		mImpl = impl;
		mResults = results;
	}
	
	public void onFire(){
	    ScanResult bestSignal = null;
	    	    
	    String currentTimeStr = new SimpleDateFormat("HH:mm:ss").format(new Date());
	    StringBuilder sb = new StringBuilder();
	    sb.append("Time: " + currentTimeStr);	 
	    
	    for (ScanResult result : mResults) {
	    	sb.append(String.format(" BSSID: %s, RSSI: %s dBm ", result.BSSID, result.level));
	    	if(ServiceManager.getPositoinService().checkMacAddr(result.BSSID) == false) continue;
	    	if (bestSignal == null
	    			|| WifiManager.compareSignalLevel(bestSignal.level, result.level) < 0)	    		
	    		bestSignal = result;
	    }
	    Log.i(TAG, sb.toString());
	    
	    if(bestSignal == null) {
	    	mImpl.StrongestWifiSignalFind(null);
	    	return;
	    }
	    
	    mImpl.StrongestWifiSignalFind(bestSignal.BSSID);
	}
}
